package com.snakerflow.framework.flow.assignmentHandler;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snaker.engine.Expression;
import org.snaker.engine.SnakerEngine;
import org.snaker.engine.SnakerInterceptor;
import org.snaker.engine.core.Execution;
import org.snaker.engine.impl.JuelExpression;
import org.snaker.engine.model.DecisionModel;
import org.snaker.engine.model.ProcessModel;

public class YMDecisionInterceptor implements SnakerInterceptor {
	private static final Logger log = LoggerFactory.getLogger(YMDecisionInterceptor.class);
	
	@Override
	public void intercept(Execution execution) {
		ProcessModel processModel = execution.getModel();
		if(processModel.containsNodeNames(DecisionModel.class,"isBoss")){
			DecisionModel decisionModel = (DecisionModel) processModel.getModels(DecisionModel.class);
			Map<String,Object> args = execution.getArgs();
			String expr = decisionModel.getExpr();
			JuelExpression expression = new JuelExpression();
			boolean isBoss = expression.eval(boolean.class, expr, args);
//			WebApplicationContext springContext = dto.getSpringContext();
			String taskName = processModel.getName();
			if(isBoss){
				execution.getEngine().task().addTaskActor(taskName, "2");
			}else{
				execution.getEngine().task().addTaskActor(taskName, "3");
			}
			log.info(decisionModel+" ================= "+taskName);
		}
		
	}

}
